package com.lelouch.bestpractice.mongorepository.tools;

import com.lelouch.bestpractice.mongorepository.resultModel.WetlandStatisticsResultByCityEntity;
import com.mongodb.BasicDBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.stereotype.Component;

import java.util.List;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;

/**
 * Mongo数据聚合操作
 *
 * @author lcy
 * @create 2018-09-13 13:26
 */
@Component
public class AggregationTools {
    @Autowired
    private MongoTemplate mongoTemplate;

    private final String COLLECTION = "WetlandBasicEntity";

    /**
     *
     * 功能：previousOperation的简单使用--为分组的字段（_id）建立别名
     * 参数：
     * 创建人：OnTheRoad_Lee
     * 修改人：OnTheRoad_Lee
     * 最后修改时间：2014-5-26
     */
//    public void testAggregation2() {
//        TypedAggregation<News> agg = Aggregation.newAggregation(
//                News.class,
////                match(Criteria.where("mediaType").is(100))
//                project("evaluate")
//                ,group("evaluate").count().as("totalNum")
//                ,project("evaluate", "totalNum")
//                        .and("eval").previousOperation()
//                //为分组的字段（_id）建立别名
//        );
//        AggregationResults<BasicDBObject> result = mongoTemplate.aggregate(agg, BasicDBObject.class);
//        System.out.println(agg.toString());
////            { "aggregate" : "__collection__" , "pipeline" : [ { "$project" : { "evaluate" : 1}} , { "$group" : { "_id" : "$evaluate" , "totalNum" : { "$sum" : 1}}} , { "$project" : { "evaluate" : "$_id.evaluate" , "totalNum" : 1 , "_id" : 0 , "eval" : "$_id"}}]}
//        System.out.println(result.getMappedResults());
////            [{ "totalNum" : 10047 , "eval" : 0}, { "totalNum" : 9955 , "eval" : 1}]
//    }


}
